<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        //Math.Max() 自实现
        /* function MyMax() {
            var max = arguments[0];
            for (let i = 1; i < arguments.length; i++) {
                if (max < arguments[i]) {
                    max = arguments[i];
                }
            }
            return max;
        }
        console.log(MyMax(1,3,2,5,6,4,7,9,8)); */

        //随机生成十六进制颜色
        /* function getColor() {
            var str = '#';
            var arr = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];
            for (let i = 0; i < 6; i++) {
                str += arr[Math.floor(Math.random()*16)];
            }
            return str;
        }
        console.log(getColor()); */

        //格式化日期
        /* function getDate() {
            var dt = new Date();
            var year = dt.getFullYear();
            var month = dt.getMonth() + 1;
            var day = dt.getDate();
            var week = dt.getDay();
            var hour = dt.getHours();
            var min = dt.getMinutes();
            var sec = dt.getSeconds();
            
            week = initWeek(week);

            month = initLtTen(month);
            day = initLtTen(day);
            hour = initLtTen(hour);
            min = initLtTen(min);
            sec = initLtTen(sec);

            var str = '现在是' + year + '年' + month + '月' + day + '日' + ',星期' +
                        week + ', ' + hour + ':' + min + ':' + sec;
            return str;
        }

        function initWeek(week) {
            switch (week) {
                case 1:
                    week = '一';
                    break;
                case 2:
                    week = '二';
                    break;
                case 3:
                    week = '三';
                    break;
                case 4:
                    week = '四';
                    break;
                case 5:
                    week = '五';
                    break;
                case 6:
                    week = '六';
                    break;
                default:
                    week = '日';
                    break;
            }
            return week;
        }

        function initLtTen(dt) {
            return dt < 10 ? '0' + dt : dt; 
        }

        console.log(getDate()); */

        //查找相对应的字符串
        // var str = '董喜炜太帅啦';
        // var key = '董喜炜';
        // var index = str.indexOf(key);
        // console.log(str.substr(index,3));
        // var str = '岳婷婷太可爱啦';
        // var key = '岳婷婷';
        // console.log(str.match(key));

        //查找想要的字符在字符串中的第几位
        /* var str = 'asdf asdf asdf asdf';
        var key = 'a';
        var index = 0;
        while ((index = str.indexOf(key,index))!== -1) {
            console.log(index);
            index++;
        }
        // for (let i = 0; i < str.length; i++) {
        //     if (key === str[i]) {
        //         console.log(i);
        //     }
        // } */

        //查找想要的字符在字符串中出现了几次 重复
        /* var str = 'hello hello hello hello hello';
        var obj = {};
        for (const val of str) {
            if (obj[val]) {
                obj[val]++;
            }else {
                obj[val] = 1;
            }
        }
        console.log(obj); */
        
        //字符串数组输出为|分割的形式 字符串转为数组
        /* var arr = ['刘备','关羽','张飞'];
        var str = arr.join('|');
        console.log(str);
        var newArr = str.split('|');
        console.log(newArr); */

        //字符数组反转
        /* var arr = ['a','b','c','d','e','f'];
        // arr.reverse();
        // console.log(arr);
        for (let i = 0; i < arr.length / 2; i++) {
            var temp = arr[i];
            arr[i] = arr[arr.length-1-i];
            arr[arr.length-1-i] = temp;
        }
        console.log(arr); */

        // 低于2000的删除
        /* var arr = [1000,2000,3000,4000,5000,60000];
        var newArr = arr.filter(function (val) {
            return val > 2000;
        });
        console.log(newArr); */

        //找到数组中每一个a出现的位置
        /* var arr = ['a','b','c','a','b','c'];
        var key = 'a';
        arr.forEach(function (val, index) {
            if (key === val) {
                console.log(index);
            }
        }); */

        //去除数组内重复元素
        var arr = ['a','a','a','b','a','b','a','c','b','b','c'];
        function uniqueArr(array){
            var r = [];
            for(var i = 0; i < array.length; i++) {
                for(var j = i + 1; j < array.length; j++){
                    if (array[i] === array[j]) {
                        j = ++i;
                    }
                }
                console.log(i);
                r.push(array[i]);
            }
            return r;
        }
        console.log(uniqueArr(arr));
    </script>
</body>
</html>